package com.vonchange.water.run;

import com.vonchange.water.domain.run.ProcessContext;
import com.vonchange.water.domain.run.ResultModel;
import com.vonchange.water.util.ScriptUtil;
import lombok.extern.slf4j.Slf4j;

import javax.script.ScriptException;
import java.util.Map;
@Slf4j
public class BaseRunModel {
    private String parentId;
    private String id;

    public BaseRunModel(String parentId, String id) {
        this.parentId = parentId;
        this.id = id;
    }


    //绑定了 groovy?
    public  ResultModel run(ProcessContext processContext,String script, Map<String,Object> args){
        if(null==script){
            return new ResultModel(true);
        }
        //发送 mq
        //生产task 审核
        // 节点流转条件返回条件
        String genId = genId();
        ResultModel result= new ResultModel(true);
        try {
            result.setResult(ScriptUtil.run(script,args));
        } catch (ScriptException e) {
            log.error("ScriptException ",e);
            result.setIsOk(false);
            result.setErrorMsg(e.getMessage());
            processContext.getErrorIds().add(genId);
        }
        processContext.getResultMap().put(genId,result);
        return result;
    }
    private String genId(){
        if(null==parentId){
            return id;
        }
        return parentId+id;
    }
}
